package com.hongyi.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hongyi.common.excel.utils.ExcelUtil;
import com.hongyi.common.framework.constant.UserConstans;
import com.hongyi.common.framework.domain.JsonResult;
import com.hongyi.common.framework.validate.AddGroup;
import com.hongyi.common.framework.validate.EditGroup;
import com.hongyi.common.log.annotation.Log;
import com.hongyi.common.log.enums.BusinessType;
import com.hongyi.common.mybatis.page.PageData;
import com.hongyi.common.mybatis.page.PageQuery;
import com.hongyi.common.web.controller.BaseController;
import com.hongyi.system.domain.SysPost;
import com.hongyi.system.domain.dto.SysPostDto;
import com.hongyi.system.domain.vo.SysConfigVo;
import com.hongyi.system.domain.vo.SysPostVo;
import com.hongyi.system.service.ISysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 岗位信息
 *
 * @author DingHao
 */
@ApiSort(value = 8)
@Api(tags = "岗位信息接口")
@Validated
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {

    @Autowired
    private ISysPostService sysPostService;

    /**
     * 获取岗位列表
     *
     * @param sysPostDto 查询条件
     * @param pageQuery 分页信息
     * @return 岗位列表分页数据
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取岗位列表")
    @SaCheckPermission("system:post:list")
    @GetMapping(value = "/list")
    public PageData<SysPostVo> list(SysPostDto sysPostDto, PageQuery pageQuery) {
        return sysPostService.selectPagePostList(sysPostDto,pageQuery);
    }

    /**
     * 岗位导出
     *
     * @param sysPostDto     岗位对象
     * @param response 返回
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "岗位导出")
    @Log(title = "岗位导出", businessType = BusinessType.EXPORT)
    @SaCheckPermission("system:post:export")
    @PostMapping(value = "/exportByStream", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void export(SysPostDto sysPostDto, HttpServletResponse response) {
        List<SysPostVo> sysPostVos = sysPostService.selectPostList(sysPostDto);
        ExcelUtil.exportExcel(sysPostVos, "岗位数据", SysPostVo.class, response);
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据岗位编号获取详情")
    @ApiImplicitParam(name = "postId", value = "岗位编号", paramType = "path", dataTypeClass = Long.class, required = true)
    @SaCheckPermission("system:post:query")
    @GetMapping(value = "/{postId}")
    public JsonResult<SysPost> getInfo(@PathVariable Long postId) {
        return JsonResult.ok(sysPostService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增岗位")
    @SaCheckPermission("system:post:add")
    @Log(title = "新增岗位", businessType = BusinessType.INSERT)
    @PostMapping
    public JsonResult<Void> add(@Validated(AddGroup.class) @RequestBody SysPostDto sysPostDto) {
        if (UserConstans.NOT_UNIQUE.equals(sysPostService.checkPostNameUnique(sysPostDto))) {
            return JsonResult.fail("新增岗位'" + sysPostDto.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstans.NOT_UNIQUE.equals(sysPostService.checkPostCodeUnique(sysPostDto))) {
            return JsonResult.fail("新增岗位'" + sysPostDto.getPostName() + "'失败，岗位编码已存在");
        }
        return toResult(sysPostService.insertPost(sysPostDto));
    }

    /**
     * 修改岗位
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改岗位")
    @SaCheckPermission("system:post:edit")
    @Log(title = "修改岗位", businessType = BusinessType.UPDATE)
    @PutMapping
    public JsonResult<Void> edit(@Validated(EditGroup.class) @RequestBody SysPostDto sysPostDto) {
        if (UserConstans.NOT_UNIQUE.equals(sysPostService.checkPostNameUnique(sysPostDto))) {
            return JsonResult.fail("修改岗位'" + sysPostDto.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstans.NOT_UNIQUE.equals(sysPostService.checkPostCodeUnique(sysPostDto))) {
            return JsonResult.fail("修改岗位'" + sysPostDto.getPostName() + "'失败，岗位编码已存在");
        }
        return toResult(sysPostService.updatePost(sysPostDto));
    }

    /**
     * 删除岗位
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "删除岗位")
    @ApiImplicitParam(name = "postIds", value = "岗位ID数组", paramType = "path", dataTypeClass = Long[].class, allowMultiple = true, required = true)
    @SaCheckPermission("system:post:remove")
    @Log(title = "删除岗位", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{postIds}")
    public JsonResult<Void> remove(@PathVariable Long[] postIds) {
        return toResult(sysPostService.deletePostByIds(postIds));
    }

}
